<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorator="base/layout.html">


    <!-- start: Content -->
    <div  layout:fragment="content" class="main ">

        <div class="row">
            <div class="col-lg-12">
                <ol class="breadcrumb">
                    <li><i class="fa fa-home"></i><a href="index.html">主页</a></li>
                    <li><i class="fa fa-list-alt"></i><a href="#">教师管理</a></li>
                    <li><i class="fa fa-indent"></i>修改教师信息 </li>
                </ol>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2><i class="fa fa-indent red"></i><strong>修改教师信息</strong></h2>
                    </div>
                    <div class="panel-body">
                        <form id="modularForm" action="" method="post" enctype="multipart/form-data" class="form-horizontal ">
                            <input type="hidden" name="id" th:value="${teacher.id}" />
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="name">姓名</label>
                                <div class="col-md-9">
                                    <input type="text" id="name" name="name" class="form-control" th:value="${teacher.name}">
                                    <span class="help-block">请输入姓名</span>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-3 control-label" for="money">工资</label>
                                <div class="col-md-9">
                                    <input type="text" id="money" name="money" class="form-control" th:value="${teacher.money}">
                                    <span class="help-block">请输入工资</span>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-3 control-label" for="subject">科目</label>
                                <div class="col-md-9">
                                    <input type="text" id="subject" name="subject" class="form-control" th:value="${teacher.subject}">
                                    <span class="help-block">请输入科目</span>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-3 control-label" for="phoneNumber">电话</label>
                                <div class="col-md-9">
                                    <input type="text" id="phoneNumber" name="phoneNumber" class="form-control" th:value="${teacher.phoneNumber}">
                                    <span class="help-block">请输入电话</span>
                                </div>
                            </div>

                            <!-- 附件相关 -->
                            <div class="form-group">
                                <label class="col-md-3 control-label">当前附件</label>
                                <div class="col-md-9">
                                    <div th:if="${teacher.attachmentName != null}">
                                        <span th:text="${teacher.attachmentName}"></span>
                                        <a th:href="@{/teachers/downloadFile(fileName=${teacher.attachmentName},originalFileName=${teacher.attachmentName})}"
                                           class="btn btn-xs btn-primary">
                                            <i class="fa fa-download"></i> 下载
                                        </a>
                                        <a href="javascript:void(0)"
                                           th:onclick="'deleteFile(\'' + ${teacher.attachmentName} + '\', ' + ${teacher.id} + ')'"
                                           class="btn btn-xs btn-danger">
                                            <i class="fa fa-trash"></i> 删除
                                        </a>
                                    </div>
                                    <div th:if="${teacher.attachmentName == null}">
                                        <span>无附件</span>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-3 control-label" for="file">更新附件</label>
                                <div class="col-md-9">
                                    <input type="file" id="file" name="file" class="form-control">
                                    <input type="hidden" id="attachmentName" name="attachmentName" th:value="${teacher.attachmentName}">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="panel-footer">
                        <button id="submitBtn" type="button" class="btn btn-sm btn-success"><i class="fa fa-dot-circle-o"></i> 提交</button>
                        <button type="reset" class="btn btn-sm btn-danger"><i class="fa fa-ban"></i> 重置</button>
                    </div>
                </div>
            </div>
        </div>

        <script>
            $("#submitBtn").click(function () {
                $.ajax({
                    url:'update',
                    type:'post',
                    data:$('#modularForm').serialize(),
                    success:function (data) {
                        layer.msg("修改成功");
                        setTimeout(function () {
                            window.location = ("teacher")
                        },1500);
                    }
                })
            });

            // 文件上传
            $("#file").change(function() {
                var formData = new FormData();
                formData.append("file", this.files[0]);
                
                $.ajax({
                    url: '/teachers/uploadFile',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(res) {
                        if(res.code == 0) {
                            $("#attachmentName").val(res.originalFileName);
                            layer.msg("上传成功");
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            });

            // 删除文件
            function deleteFile(fileName, teacherId) {
                layer.confirm('确定要删除此附件吗？', {
                    btn: ['确定','取消']
                }, function(){
                    $.ajax({
                        url: '/teachers/deleteFile',
                        type: 'POST',
                        data: {
                            fileName: fileName,
                            teacherId: teacherId
                        },
                        success: function(res) {
                            if(res.code == 0) {
                                $("#attachmentName").val('');
                                layer.msg("删除成功");
                                location.reload();
                            } else {
                                layer.msg(res.msg);
                            }
                        }
                    });
                });
            }
        </script>
    </div>
    <!-- end: Content -->

